// index.js
// 获取应用实例
const md5 = require("@/utils/md5.js");
const request = require('@/utils/request.js')

Page({
    data: {
        userInfo: {},
        accountNo: "",
        empPwd: "",
        isLogin: false,
        accountNoError: '',
        empPwdError: '',
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        const app = getApp();
    },
    onLoad() {
        wx.hideHomeButton(); //隐藏home/返回主页按钮
        const app = getApp();
        this.setData({
            loginImg: app.globalData.nginxIp + "/pages/img/login.png",
            liangyouImg: app.globalData.nginxIp + "/pages/img/liangyou.png",
            zhImg: app.globalData.nginxIp + "/pages/img/zh.png",
            mmImg: app.globalData.nginxIp + "/pages/img/mm.png"
        })
    },
    onShow() {

    },
    getPhoneNumber(e) {
        console.log(e.detail.code) // 动态令牌
        console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
        console.log(e.detail.errno) // 错误码（失败时返回）
    },

    onInput(e) {
      let name = e.currentTarget.dataset.name
      let value = e.detail.value
      if (value) {
        this.setData({
          [`${name}Error`]: ''
        })
      }
    },

    /**
     * 登录
     */
    formSubmit(e) {
        const app = getApp();
        if (e.detail.value.accountNo == null || e.detail.value.accountNo == '') {
            this.setData({
                accountNoError: "error"
            })
            return 
        }
        if (e.detail.value.empPwd == null || e.detail.value.empPwd == '') {
            this.setData({
              empPwdError: "error"
            })
            return 
        }
        let SysEmp={}
        SysEmp.accountNo=e.detail.value.accountNo
        SysEmp.empPwd=md5.hexMD5(e.detail.value.empPwd)
        request.post('/HomeController/login',SysEmp).then(res => {
            console.log(res)
            if(res.data.code == "200"){
                app.globalData.userInfo=res.data.result.userInfo
                wx.setStorageSync("userInfo",res.data.result.userInfo)
                wx.switchTab({
                  url: '/pages/home/home',
                })
            }else{
              wx.showToast({
                title: res.data.message || '登录失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
    },
    durationEnd() {

    },
    login(){
        wx.navigateTo({
          url: '/pages/logon/logon',
        })
    },
    forgetPassword(){
        wx.navigateTo({
          url: '/pages/forgetPassword/forgetPassword',
        })
    }
})